package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class StrengthenTab extends TechTab
	{
		public var strengthenSlot:ObjectSlot;
		
		public function StrengthenTab()
		{
			super();
			strengthenSlot = new ObjectSlot();
			strengthenSlot.x = 250;
			strengthenSlot.y = 100;
//			strengthenSlot.width = 60;
//			strengthenSlot.height = 50;
			
			addChild(originalSlot);
			addChild(strengthenSlot);
			//箭头
			var RightArrow:Class = ResourceHelper.getInstance().getClassByName("RightArrow3");
			var rightArrow:Sprite = new RightArrow();
			rightArrow.x = 130;
			rightArrow.y = 110;
			addChild(rightArrow);
			//强化按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			startButton = new BasicButton(ButtonBgr, "立即强化");
			startButton.x = 223;
			startButton.y = 375;
			startButton.disable = true;
			addChild(startButton);
			
			startButton.addEventListener(MouseEvent.CLICK, onStartButtonClick)
		}
		
		private function onStartButtonClick(event:MouseEvent):void
		{
			if (startButton.disable)
				return;
			if (originalSlot.strengthenLevel >= 4)
				return;
			var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
			var objBalance:String = this.generateObjBalance();
			var successRate:int = parseInt(successRateText2.text);
			var successFlag:Boolean;
			if (Math.random() < successRate / 100)
				successFlag = true;
			else
				successFlag = false;
			//坦克
			if (originalObjConfig.type == 0)
			{
				ServerHelper.getInstance().strengthenTank(originalSlot.tankId, originalSlot.objectIndex, originalSlot.basicLevel, 
					originalSlot.strengthenLevel, objBalance, successFlag);
				ServerHelper.getInstance().addEventListener(ServerEvent.STRENGTHEN_TANK, strengthComplete);
			}
			else
			{
				ServerHelper.getInstance().strengthenObj(originalSlot.objectIndex, originalSlot.basicLevel, originalSlot.strengthenLevel,
					originalSlot.tankId, objBalance, successFlag);
				ServerHelper.getInstance().addEventListener(ServerEvent.STRENGTHEN, strengthComplete);
			}
			removeAll();
		}
		
		private function strengthComplete(event:ServerEvent):void
		{
			var popMessage:PopMessage;
			if (event.result == "success")
			{
				popMessage = new PopMessage("强化成功！", 0x00ff00);
			}
			else
			{
				popMessage = new PopMessage("强化失败！", 0xff0000);
			}
			WidgetManager.getInstance().popOut([popMessage], false, true);
			Warehouse.getInstance().refresh();
		}
		
		public override function loadObject(objectIndex:int, basicLevel:int, strengthenLevel:int, tankId:int = 0):void
		{
			super.loadObject(objectIndex, basicLevel, strengthenLevel);
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			//卸下原来的物品
			removeAll();
			//刷新界面
			originalSlot.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1, tankId);
			//刷新包裹
			if (objectConfig.type == 0)
			{
				me.tankPackage.deleteTank(tankId);
			}
			else
			{
				me.objPackage.objectList[originalSlot.comIndex]--;
			}
			if (objectConfig.noBasicLevel || objectConfig.noStrengthenLevel)
			{
				warningTip.text = "该物品无法进行强化!";
				startButton.disable = true;
			}
			else if (strengthenLevel >= 4)
			{
				warningTip.text = "已到达最高等级，无法继续强化!";
				startButton.disable = true;
			}
			else
			{
				startButton.disable = false;
				strengthenSlot.loadObj(objectIndex, basicLevel, strengthenLevel + 1, -1, 1, tankId);	
				loadMaterial();
			}
		}
		
		public override function removeAll():void
		{
			super.removeAll();
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (originalSlot.hasObj)
			{
				var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
				if (originalObjConfig.type == 1 || originalObjConfig.type == 2)
				{
					me.objPackage.objectList[originalSlot.comIndex]++;
				}
				else if (originalObjConfig.type == 0)
				{
					me.tankPackage.tankList.push(originalSlot.tankId);
				}
				originalSlot.removeObj();
			}
			strengthenSlot.removeObj();
		}
	}
}